package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Example16 {
    public static void main(String[] args) {
        JFrame f = new JFrame("JFrame窗口");
        f.setSize(350,200);
        f.setLocation(300,200);
        f.setLayout(new BorderLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JComboBox<String> comboBox = new JComboBox<>();
        comboBox.addItem("请选择城市");
        comboBox.addItem("北京");
        comboBox.addItem("天津");
        comboBox.addItem("南京");
        comboBox.addItem("上海");
        JTextField textField = new JTextField(20);
        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String item=(String)comboBox.getSelectedItem();
                if("请选择城市".equals(item)){
                    textField.setText("");
                }
                else{
                    textField.setText("您选择的城市是"+item);
                }
            }});
        panel.add(comboBox);
        panel.add(textField);
        f.add(panel,BorderLayout.PAGE_START);
        f.setVisible(true);
    }
}
